﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WHM3
{
    public static class BL
    {
        private static bool initialized;

        public static void init()
        {
            if (initialized) { return; }
            DAL.init();
            initialized = true;
        }
        
        
        public static Employee getEmployeeById(string ID)
        {
            if (!initialized)
                init();
           
            try
            {
                return DAL.getEmployeeById(ID);
            }
            catch (MySql.Data.MySqlClient.MySqlException)
            {
                return null;
            }
            catch (Exception)
            {
                throw new ArgumentNullException();
            }
        }

        public static Employee getEmployeeByLoginDetails(LoginDetails ld)
        {
            if (!initialized)
                init();

            try
            {
                return DAL.getEmployeeById(ld.ID);
            }
            catch (MySql.Data.MySqlClient.MySqlException)
            {
                return null;
            }
            catch (Exception)
            {
                throw new ArgumentNullException();
            }
        }

        public static bool addEmployeeToDB(Employee emp)
        {
            if (!initialized)
                init();
            
            try
            {
                DAL.addEmployee(emp);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static LinkedList<Shift> getAllShiftsById(string ID)
        {
            if (!initialized)
                init();

            LinkedList<Shift> list;

            try
            {
                list = DAL.getAllShiftsById(ID);
            }
            catch
            {
                return null;
            }
            return list;
        }

        public static LinkedList<Shift> getAllShiftsByIdAndDate(string ID,string date)
        {
            if (!initialized)
                init();

            LinkedList<Shift> list;
            LinkedList<Shift> ret = new LinkedList<Shift>();
            try
            {
                list = DAL.getAllShiftsById(ID);
            }
            catch
            {
                return null;
            }

            foreach (Shift shift in list)
            {
                string d = shift.getStartDay() + "/" + shift.getStartMonth() + "/" + shift.getStartYear();
                if (d.Equals(date)) { ret.AddFirst(shift); }
            }

            return ret;
        }

        public static bool changePassword(Employee emp, String newPass)
        {
            if (!initialized)
                init();

            try
            {
                DAL.changePassword(emp, newPass);
            }
            catch (MySql.Data.MySqlClient.MySqlException)
            {
                return false;
            }
            catch (Exception)
            {
                throw new ArgumentNullException();
            }
            return true;

        }

        public static bool addNewShift(string id, string start, string end)
        {
            if (!(new Shift(id, start, end).isInCorrectFormat())) { return false; }
            if (end == null) { end = ""; }

            try
            {
                DAL.addShift(id, start, end);
            }
            catch
            {
                return false;
            }

            return true;            
        }

        public static bool checkIntoWork(string id)
        {
            string startTime = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString()
                + "::" + DateTime.Now.Hour + ":" + DateTime.Now.Minute.ToString();

            try
            {

                if (DAL.checkIntoWork(id, startTime))
                {
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public static bool updateEmployee(Employee employee)
        {
            if(employee.id == null){return false;}
            if (employee.firstName == null) { employee.firstName = ""; }
            if (employee.lastName == null) { employee.lastName = ""; }
            if (employee.Password == null) { return false; }
            if (employee.Position == null) { employee.Position = ""; }
            if (employee.Team == null) { employee.Team = ""; }
            if (employee.isAdmin == null) { employee.isAdmin = ""; }
            if (employee.isManager == null) { employee.isManager = "False"; employee.managerOf = ""; }
            if (employee.managerOf == null) { employee.managerOf = ""; }
            if (employee.minHours == null) { employee.minHours = "186"; }

            try
            {
                DAL.updateEmployee(employee);
            }
            catch (MySql.Data.MySqlClient.MySqlException)
            {
                return false;
            }
            catch (Exception)
            {
                throw new ArgumentNullException();
            }
            return true;
        }

        public static bool checkOutOfWork(string id)
        {
            string endTime = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString()
                + "::" + DateTime.Now.Hour + ":" + DateTime.Now.Minute.ToString();

            try
            {
                if (DAL.checkOutOfWork(id, endTime))
                {
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public static String getLastOpenShift(string id)
        {
            string today = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();

            LinkedList<Shift> list;
            try
            {
                list = BL.getAllShiftsByIdAndDate(id, today);
            }
            catch
            {
                return null;
            }

            if (list == null || list.Count == 0)
            {
                return null;
            }

            foreach(Shift s in list)
            {
                if (s.getStartDay() != null && s.getEndDay() == null)
                {
                    return s.Start;
                }
            }

            return null;
        }
        public static bool updateShift(Shift befor, Shift after)
        {
            if (befor.id == null || befor.Start == null || after.id == null || after.Start == null)
            {
                return false;
            }
            if (befor.End == null)
            {
                befor.End = "";
            }
            if (after.End == null)
            {
                after.End = "";
            }

            try
            {
                DAL.updateShift(befor, after);
            }
            catch (MySql.Data.MySqlClient.MySqlException)
            {
                return false;
            }
            catch (Exception)
            {
                throw new ArgumentNullException();
            }
            return true;
        }

        public static List<Employee> getAllEmployeesByTeam(string team,Boolean isAdmin)
        {

            if (!initialized)
                init();

            try
            {
                return DAL.getAllEmployeesByTeam(team,isAdmin);
            }
            catch
            {
                return null;
            }

            //no exception but no Employees...
            throw new NotImplementedException();

        }

        public static List<ChangeLogEntry> getChangeLogForID(string id)
        {

            if (!initialized)
                init();

            List<ChangeLogEntry> fullList = null;
            try
            {
                fullList = BL.getChangeLogForAll();
            }
            catch
            {
                throw new ArgumentNullException();
            }

            List<ChangeLogEntry> list = new List<ChangeLogEntry>();

            foreach (ChangeLogEntry c in fullList)
            {
                if (c.ID.Equals(id))
                {
                    list.Add(c);
                }
            }

            return list;
        }

        public static List<ChangeLogEntry> getChangeLogForTeam(string team)
        {
            if (!initialized)
                init();

            List<ChangeLogEntry> fullList = null;
            try
            {
                fullList = BL.getChangeLogForAll();
            }
            catch
            {
                throw new ArgumentNullException();
            }

            List<ChangeLogEntry> list = new List<ChangeLogEntry>();

            foreach (ChangeLogEntry c in fullList)
            {
                if (c.team.Equals(team))
                {
                    list.Add(c);
                }
            }

            return list;
        }

        public static List<ChangeLogEntry> getChangeLogForAll()
        {
            try
            {
                return DAL.getChangeLog();
            }
            catch
            {
                throw new ArgumentNullException();
            }

        }

        public static bool deleteEmployee(Employee employee)
        {
            try
            {
                DAL.deleteEmployee(employee);
            }
            catch (MySql.Data.MySqlClient.MySqlException)
            {
                return false;
            }
            catch (Exception)
            {
                throw new ArgumentNullException();
            }
            return true;
        }
    }
}